<?php
namespace Modules\Admin\Http\Controllers;
use Illuminate\Http\Request;
use Modules\Admin\Http\Requests\CommonStatusRequest;
use Modules\Admin\Http\Requests\CommonSortRequest;
use Modules\Admin\Http\Requests\CommonDeleteRequest;
use Modules\Admin\Http\Requests\CommonDeleteAllRequest;
use Modules\Admin\Models\Picture as PictureModel;
use Modules\Admin\Models\Image as ImageModel;
use Modules\ShopAdmin\Models\ShopProduct as ShopProductModel;
use Modules\Admin\Http\Controllers\CommonController;
class PictureController extends CommonController
{
	/**
	 * @name  列表
	 */
    public function index()
    { 
		$status = PictureModel::common_status();
		$type_lists = (new PictureModel())->type_lists();
        return view('admin::picture.index',compact('type_lists','status'));
    }
	/**
	 * @name  列表数据
	 */
	 
    public function data(Request $request)
    {
	    $model = PictureModel::query();
		$params = $request->all();
		$model = query_condition($model,$params);
		if (isset($params['type']) && $params['type'] > 0){
            $model = $model->where('type',$params['type']);
        }
        $res = $model->with('image_one')
				->orderBy('sort','asc')
				->orderBy('type','asc')
				->orderBy('id','desc')  
				->paginate($params['limit'])
				->toArray(); 
		$res['data'] = (new PictureModel())->data_image($res['data']);	
        return response()->json(['code' => 0, 'msg'   => config('admin.limit_msg'),'count' => $res['total'],'data'  => $res['data'],'rel'=>1]);
    }
	/**
	 * @name  添加页面
	 */
    public function create()
    { 
		$picture_model 			= new PictureModel();
		$type_lists 			= $picture_model->type_lists();
		$product_status_list 	= $picture_model->product_status_list();
		$shop_product_list 		= (new ShopProductModel())->where('status',1)->select('id','name')->get();
		return view('admin::picture.create',compact('type_lists','product_status_list','shop_product_list'));
    }
	/**
	 * @name  添加数据处理 
	 */
    public function store(Request $request)
    {
		$data = $request->all();
		$data = (new ImageModel())->add_image($data,'file');
		if(!is_array($data)){
			return $data;
		};
		return (new PictureModel())->common_create($data,route('admin.picture.index'));  
    }
	/**
	 * @name  编辑页面
	 */
    public function edit($id)
    {
		$picture_model 			= new PictureModel();
		$type_lists 			= $picture_model->type_lists();
		$product_status_list 	= $picture_model->product_status_list();
		$shop_product_list 		= (new ShopProductModel())->where('status',1)->select('id','name')->get();
		$info = PictureModel::with('image_one')->find($id)->toArray();
		return view('admin::picture.edit',compact('info','type_lists','product_status_list','shop_product_list'));
    }
	/**
	 * @name  编辑数据处理
	 */
    public function update(Request $request,$id)
    {
		$data = $request->all();
		$data = (new ImageModel())->edit_image('pictures',$id,$data,'file');
		if(!is_array($data)){
			return $data;
		};
		return (new PictureModel())->common_update($data,$id,route('admin.picture.index')); 
    }
	/**
	 * @name  调整菜单状态数据处理
	 */
    public function status(CommonStatusRequest $request,$id)
    {
		return (new PictureModel())->common_status_update($request->all(),$id,route('admin.picture.index')); 
    }
	/**
	 * @name  排序数据处理
	 */
    public function sorts(CommonSortRequest $request,$id)
    {
		return (new PictureModel())->common_status_update($request->all(),$id,route('admin.picture.index'));  
    }
	/**
	 * @name  删除数据处理
	 */
    public function destroy(CommonDeleteRequest $request)
    {
		$id = (new ImageModel())->del_image('pictures',$request->get('id'));
		if(!is_numeric($id)){
			return $id;
		};
		return (new PictureModel())->common_destroy($id,route('admin.picture.index'));  
    }
	/**
	 * @name  批量删除数据处理
	 */
    public function destroy_all(CommonDeleteAllRequest $request)
    {
		$ids = (new ImageModel())->del_all_image('pictures',$request->get('ids'));
		if(!is_array($ids)){
			return $ids;
		};
		return (new PictureModel())->common_destroy($ids,route('admin.picture.index'));  
    }
	
	
	
	
	
}
